<div class="page-wrapper">
    <div class="header">
        <a href="javascript:history.back();" class="left"><i class="icon-back"></i><span class="qr_header_text">订单</span></a>
        <div id="order-preview-title" class="title">收款</div>
    </div>

    <div class="order-body">
        <div class="qr-wrapper">
            <div class="qr-content">
                <div id="qrCode" class="qr">
                </div>
                <div class="message">请扫描二维码付款</div>

            </div>
            <div class="success">
                <i class="success-icon icon-selected"></i>
                <div>支付成功</div>
            </div>
        </div>
    </div>
    <script>
        (function(window,document,$){
            'use strict';
            var from = spa.getParam('from');
            $('.header .qr_header_text').text(from || '订单');

            var tId = spa.getParam('tableId');

            //获取支付的地址
            $.getJSON('/wechat/payQrCode/'+tId,'',function(data){
                console.log('getJSON /wechat/payQrCode/');
                console.log(data);
                if(data && data.qrCodeUrl){
                    showQr(data.qrCodeUrl);
                }else{
                    showQr(undefined);
                }
            }).fail(function(){
                showQr(undefined);
            });

            function showQr(url) {
                console.log('showQr ' + url);
                if(!url){
                    //获取支付数据失败
                    common.toast('获取支付数据失败');
                    var action=spa.getParam('action');
                    if(action == 'waiter_qr'){
                        setTimeout(function(){
                            history.back();
                        },2000);
                    }
                    return;
                }

                var width = 200;
                var height = 200;
                var text = url;// || 'http://www.baidu.com';
                console.log(text);
                $('#qrCode').qrcode({
                    render:'canvas',
                    width:width,
                    height:height,
                    text:text
                });

                isPaySuccess();

            }
            var tryTime = 30;//尝试30次获取结果
            var timerId = undefined;
            function isPaySuccess(){
                timerId = setTimeout(function(){
                    if(tryTime > 0){
                        tryTime --;
                        orderByTableId();
                    }else{//支付超时
                        common.toast('长时间未支付(^-^)');
                    }
                },2000);
            }
            //离开本页面之前，取消timer事件
            spa.addBeforeLeaveCallback(function(){
                console.log('waiter_qr before leave');
               if(timerId && tryTime > 0){
                   clearTimeout(timerId);
                   tryTime = 0;
               }
            });


            function orderByTableId() {
                console.log(tId);
                $.get('/waiterOrder/api/getCurrentTableOrder/' + tId, function (data) {
                    console.log(data);
                    if(data && data.state != 1 ){//&& tryTime != 0){
                        isPaySuccess();
                    }else{ //支付成功
                        console.log('finished ');
                        tryTime = 0;
                        clearTimeout(timerId);
                        $('.qr-content').hide();
                        $('.success').show();
                    }
                });
            }

        }(window,document,$));
    </script>
</div>